home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / pr-output.cc < prev    next >
C/C++ Source or Header  |  1997-05-26  |  34KB  |  1,773 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <cfloat>
  28. #include <cstdio>
  29. #include <cstring>
  30.  
  31. #include <string>
  32.  
  33. #include <iostream.h>
  34. #include <strstream.h>
  35.  
  36. #include "CMatrix.h"
  37. #include "Range.h"
  38. #include "dMatrix.h"
  39. #include "mach-info.h"
  40. #include "oct-cmplx.h"
  41. #include "oct-math.h"
  42. #include "oct-term.h"
  43. #include "str-vec.h"
  44.  
  45. #include "defun.h"
  46. #include "error.h"
  47. #include "gripes.h"
  48. #include "help.h"
  49. #include "mappers.h"
  50. #include "oct-obj.h"
  51. #include "pager.h"
  52. #include "pr-output.h"
  53. #include "sysdep.h"
  54. #include "utils.h"
  55. #include "variables.h"
  56.  
  57. // The maximum field width for a number printed by the default output
  58. // routines.
  59. static int Voutput_max_field_width;
  60.  
  61. // The precision of the numbers printed by the default output
  62. // routines.
  63. static int Voutput_precision;
  64.  
  65. // TRUE means that the dimensions of empty matrices should be printed
  66. // like this: x = [](2x0).
  67. static bool Vprint_empty_dimensions;
  68.  
  69. // TRUE means that the rows of big matrices should be split into
  70. // smaller slices that fit on the screen.
  71. static bool Vsplit_long_rows;
  72.  
  73. // Current format string for real numbers and the real part of complex
  74. // numbers.
  75. static char *curr_real_fmt = 0;
  76.  
  77. // Current format string for the imaginary part of complex numbers.
  78. static char *curr_imag_fmt = 0;
  79.  
  80. // Nonzero means don't do any fancy formatting.
  81. static bool free_format = false;
  82.  
  83. // Nonzero means print plus sign for nonzero, blank for zero.
  84. static bool plus_format = false;
  85.  
  86. // Nonzero means always print like dollars and cents.
  87. static bool bank_format = false;
  88.  
  89. // Nonzero means print data in hexadecimal format.
  90. static bool hex_format = false;
  91.  
  92. // Nonzero means print data in binary-bit-pattern format.
  93. static int bit_format = 0;
  94.  
  95. // Nonzero means don't put newlines around the column number headers.
  96. static bool compact_format = false;
  97.  
  98. // Nonzero means use an e format.
  99. static bool print_e = false;
  100.  
  101. // Nonzero means print E instead of e for exponent field.
  102. static bool print_big_e = false;
  103.  
  104. // XXX FIXME XXX -- these should probably be somewhere else.
  105.  
  106. static double
  107. pr_max_internal (const Matrix& m)
  108. {
  109.   int nr = m.rows ();
  110.   int nc = m.columns ();
  111.  
  112.   double result = DBL_MIN;
  113.  
  114.   for (int j = 0; j < nc; j++)
  115.     for (int i = 0; i < nr; i++)
  116.       {
  117.     double val = m (i, j);
  118.     if (xisinf (val) || xisnan (val))
  119.       continue;
  120.  
  121.     if (val > result)
  122.       result = val;
  123.       }
  124.   return result;
  125. }
  126.  
  127. static double
  128. pr_min_internal (const Matrix& m)
  129. {
  130.   int nr = m.rows ();
  131.   int nc = m.columns ();
  132.  
  133.   double result = DBL_MAX;
  134.  
  135.   for (int j = 0; j < nc; j++)
  136.     for (int i = 0; i < nr; i++)
  137.       {
  138.     double val = m (i, j);
  139.     if (xisinf (val) || xisnan (val))
  140.       continue;
  141.  
  142.     if (val < result)
  143.       result = val;
  144.       }
  145.   return result;
  146. }
  147.  
  148. // XXX FIXME XXX -- it would be nice to share more code among these
  149. // functions,..
  150.  
  151. static void
  152. set_real_format (bool sign, int digits, bool inf_or_nan, bool nan_or_int,
  153.          int &fw)
  154. {
  155.   static char fmt_buf[128];
  156.  
  157.   int prec = Voutput_precision;
  158.  
  159.   int ld, rd;
  160.  
  161.   if (bank_format)
  162.     {
  163.       fw = digits < 0 ? 4 : digits + 3;
  164.       if (inf_or_nan && fw < 3)
  165.     fw = 3;
  166.       fw += sign;
  167.       rd = 2;
  168.     }
  169.   else if (hex_format)
  170.     {
  171.       fw = 2 * sizeof (double);
  172.       rd = 0;
  173.     }
  174.   else if (bit_format)
  175.     {
  176.       fw = 8 * sizeof (double);
  177.       rd = 0;
  178.     }
  179.   else if (nan_or_int)
  180.     {
  181.       fw = digits;
  182.       if (inf_or_nan && fw < 3)
  183.     fw = 3;
  184.       fw += sign;
  185.       rd = 0;
  186.     }
  187.   else
  188.     {
  189.       if (digits > 0)
  190.     {
  191.       ld = digits;
  192.       rd = prec > digits ? prec - digits : prec;
  193.       digits++;
  194.     }
  195.       else
  196.     {
  197.       ld = 1;
  198.       rd = prec > digits ? prec - digits : prec;
  199.       digits = -digits + 1;
  200.     }
  201.  
  202.       fw = ld + 1 + rd;
  203.       if (inf_or_nan && fw < 3)
  204.     fw = 3;
  205.       fw += sign;
  206.     }
  207.  
  208.   if (! (bank_format || hex_format || bit_format)
  209.       && (fw > Voutput_max_field_width || print_e))
  210.     {
  211.       int exp_field = 4;
  212.       if (digits > 100)
  213.     exp_field++;
  214.  
  215.       fw = 2 + prec + exp_field;
  216.       if (inf_or_nan && fw < 3)
  217.     fw = 3;
  218.       fw += sign;
  219.  
  220.       if (print_big_e)
  221.     sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1);
  222.       else
  223.     sprintf (fmt_buf, "%%%d.%de", fw, prec - 1);
  224.     }
  225.   else
  226.     {
  227.       sprintf (fmt_buf, "%%%d.%df", fw, rd);
  228.     }
  229.  
  230.   curr_real_fmt = &fmt_buf[0];
  231. }
  232.  
  233. static void
  234. set_format (double d, int& fw)
  235. {
  236.   curr_real_fmt = 0;
  237.   curr_imag_fmt = 0;
  238.  
  239.   if (free_format)
  240.     return;
  241.  
  242.   bool sign = (d < 0.0);
  243.  
  244.   bool inf_or_nan = (xisinf (d) || xisnan (d));
  245.  
  246.   bool nan_or_int = (xisnan (d) || D_NINT (d) == d);
  247.  
  248.   double d_abs = d < 0.0 ? -d : d;
  249.  
  250.   int digits = (inf_or_nan || d_abs == 0.0) ? 0
  251.     : (int) floor (log10 (d_abs) + 1.0);
  252.  
  253.   set_real_format (sign, digits, inf_or_nan, nan_or_int, fw);
  254. }
  255.  
  256. static inline void
  257. set_format (double d)
  258. {
  259.   int fw;
  260.   set_format (d, fw);
  261. }
  262.  
  263. static void
  264. set_real_matrix_format (bool sign, int x_max, int x_min,
  265.             bool inf_or_nan, int int_or_inf_or_nan, int& fw)
  266. {
  267.   static char fmt_buf[128];
  268.  
  269.   int prec = Voutput_precision;
  270.  
  271.   int ld, rd;
  272.  
  273.   if (bank_format)
  274.     {
  275.       int digits = x_max > x_min ? x_max : x_min;
  276.       fw = digits <= 0 ? 4 : digits + 3;
  277.       if (inf_or_nan && fw < 3)
  278.     fw = 3;
  279.       fw += sign;
  280.       rd = 2;
  281.     }
  282.   else if (hex_format)
  283.     {
  284.       fw = 2 * sizeof (double);
  285.       rd = 0;
  286.     }
  287.   else if (bit_format)
  288.     {
  289.       fw = 8 * sizeof (double);
  290.       rd = 0;
  291.     }
  292.   else if (int_or_inf_or_nan)
  293.     {
  294.       int digits = x_max > x_min ? x_max : x_min;
  295.       fw = digits <= 0 ? 1 : digits;
  296.       if (inf_or_nan && fw < 3)
  297.     fw = 3;
  298.       fw += sign;
  299.       rd = 0;
  300.     }
  301.   else
  302.     {
  303.       int ld_max, rd_max;
  304.       if (x_max > 0)
  305.     {
  306.       ld_max = x_max;
  307.       rd_max = prec > x_max ? prec - x_max : prec;
  308.       x_max++;
  309.     }
  310.       else
  311.     {
  312.       ld_max = 1;
  313.       rd_max = prec > x_max ? prec - x_max : prec;
  314.       x_max = -x_max + 1;
  315.     }
  316.  
  317.       int ld_min, rd_min;
  318.       if (x_min > 0)
  319.     {
  320.       ld_min = x_min;
  321.       rd_min = prec > x_min ? prec - x_min : prec;
  322.       x_min++;
  323.     }
  324.       else
  325.     {
  326.       ld_min = 1;
  327.       rd_min = prec > x_min ? prec - x_min : prec;
  328.       x_min = -x_min + 1;
  329.     }
  330.  
  331.       ld = ld_max > ld_min ? ld_max : ld_min;
  332.       rd = rd_max > rd_min ? rd_max : rd_min;
  333.  
  334.       fw = ld + 1 + rd;
  335.       if (inf_or_nan && fw < 3)
  336.     fw = 3;
  337.       fw += sign;
  338.     }
  339.  
  340.   if (! (bank_format || hex_format || bit_format)
  341.       && (fw > Voutput_max_field_width || print_e))
  342.     {
  343.       int exp_field = 4;
  344.       if (x_max > 100 || x_min > 100)
  345.     exp_field++;
  346.  
  347.       fw = 2 + prec + exp_field;
  348.       if (inf_or_nan && fw < 3)
  349.     fw = 3;
  350.       fw += sign;
  351.  
  352.       if (print_big_e)
  353.     sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1);
  354.       else
  355.     sprintf (fmt_buf, "%%%d.%de", fw, prec - 1);
  356.     }
  357.   else
  358.     {
  359.       sprintf (fmt_buf, "%%%d.%df", fw, rd);
  360.     }
  361.  
  362.   curr_real_fmt = &fmt_buf[0];
  363. }
  364.  
  365. static void
  366. set_format (const Matrix& m, int& fw)
  367. {
  368.   curr_real_fmt = 0;
  369.   curr_imag_fmt = 0;
  370.  
  371.   if (free_format)
  372.     return;
  373.  
  374.   bool sign = m.any_element_is_negative ();
  375.  
  376.   bool inf_or_nan = m.any_element_is_inf_or_nan ();
  377.  
  378.   bool int_or_inf_or_nan = m.all_elements_are_int_or_inf_or_nan ();
  379.  
  380.   Matrix m_abs = m.abs ();
  381.   double max_abs = pr_max_internal (m_abs);
  382.   double min_abs = pr_min_internal (m_abs);
  383.  
  384.   int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0);
  385.   int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0);
  386.  
  387.   set_real_matrix_format (sign, x_max, x_min, inf_or_nan,
  388.               int_or_inf_or_nan, fw);
  389. }
  390.  
  391. static inline void
  392. set_format (const Matrix& m)
  393. {
  394.   int fw;
  395.   set_format (m, fw);
  396. }
  397.  
  398. static void
  399. set_complex_format (bool sign, int x_max, int x_min, int r_x,
  400.             bool inf_or_nan, int int_only, int& r_fw, int& i_fw)
  401. {
  402.   static char r_fmt_buf[128];
  403.   static char i_fmt_buf[128];
  404.  
  405.   int prec = Voutput_precision;
  406.  
  407.   int ld, rd;
  408.  
  409.   if (bank_format)
  410.     {
  411.       int digits = r_x;
  412.       i_fw = 0;
  413.       r_fw = digits <= 0 ? 4 : digits + 3;
  414.       if (inf_or_nan && r_fw < 3)
  415.     r_fw = 3;
  416.       r_fw += sign;
  417.       rd = 2;
  418.     }
  419.   else if (hex_format)
  420.     {
  421.       r_fw = 2 * sizeof (double);
  422.       i_fw = 2 * sizeof (double);
  423.       rd = 0;
  424.     }
  425.   else if (bit_format)
  426.     {
  427.       r_fw = 8 * sizeof (double);
  428.       i_fw = 8 * sizeof (double);
  429.       rd = 0;
  430.     }
  431.   else if (inf_or_nan || int_only)
  432.     {
  433.       int digits = x_max > x_min ? x_max : x_min;
  434.       i_fw = r_fw = digits <= 0 ? 1 : digits;
  435.       if (inf_or_nan && i_fw < 3)
  436.     i_fw = r_fw = 3;
  437.       r_fw += sign;
  438.       rd = 0;
  439.     }
  440.   else
  441.     {
  442.       int ld_max, rd_max;
  443.       if (x_max > 0)
  444.     {
  445.       ld_max = x_max;
  446.       rd_max = prec > x_max ? prec - x_max : prec;
  447.       x_max++;
  448.     }
  449.       else
  450.     {
  451.       ld_max = 1;
  452.       rd_max = prec > x_max ? prec - x_max : prec;
  453.       x_max = -x_max + 1;
  454.     }
  455.  
  456.       int ld_min, rd_min;
  457.       if (x_min > 0)
  458.     {
  459.       ld_min = x_min;
  460.       rd_min = prec > x_min ? prec - x_min : prec;
  461.       x_min++;
  462.     }
  463.       else
  464.     {
  465.       ld_min = 1;
  466.       rd_min = prec > x_min ? prec - x_min : prec;
  467.       x_min = -x_min + 1;
  468.     }
  469.  
  470.       ld = ld_max > ld_min ? ld_max : ld_min;
  471.       rd = rd_max > rd_min ? rd_max : rd_min;
  472.  
  473.       i_fw = r_fw = ld + 1 + rd;
  474.       if (inf_or_nan && i_fw < 3)
  475.     i_fw = r_fw = 3;
  476.       r_fw += sign;
  477.     }
  478.  
  479.   if (! (bank_format || hex_format || bit_format)
  480.       && (r_fw > Voutput_max_field_width || print_e))
  481.     {
  482.       int exp_field = 4;
  483.       if (x_max > 100 || x_min > 100)
  484.     exp_field++;
  485.  
  486.       i_fw = r_fw = 1 + prec + exp_field;
  487.       if (inf_or_nan && i_fw < 3)
  488.     i_fw = r_fw = 3;
  489.       r_fw += sign;
  490.  
  491.       if (print_big_e)
  492.     {
  493.       sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1);
  494.       sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1);
  495.     }
  496.       else
  497.     {
  498.       sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1);
  499.       sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1);
  500.     }
  501.     }
  502.   else
  503.     {
  504.       sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd);
  505.       sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd);
  506.     }
  507.  
  508.   curr_real_fmt = &r_fmt_buf[0];
  509.   curr_imag_fmt = &i_fmt_buf[0];
  510. }
  511.  
  512. static void
  513. set_format (const Complex& c, int& r_fw, int& i_fw)
  514. {
  515.   curr_real_fmt = 0;
  516.   curr_imag_fmt = 0;
  517.  
  518.   if (free_format)
  519.     return;
  520.  
  521.   double rp = c.real ();
  522.   double ip = c.imag ();
  523.  
  524.   bool sign = (rp < 0.0);
  525.  
  526.   bool inf_or_nan = (xisinf (c) || xisnan (c));
  527.  
  528.   bool int_only = (D_NINT (rp) == rp && D_NINT (ip) == ip);
  529.  
  530.   double r_abs = rp < 0.0 ? -rp : rp;
  531.   double i_abs = ip < 0.0 ? -ip : ip;
  532.  
  533.   int r_x = r_abs == 0.0 ? 0 : (int) floor (log10 (r_abs) + 1.0);
  534.   int i_x = i_abs == 0.0 ? 0 : (int) floor (log10 (i_abs) + 1.0);
  535.  
  536.   int x_max, x_min;
  537.  
  538.   if (r_x > i_x)
  539.     {
  540.       x_max = r_x;
  541.       x_min = i_x;
  542.     }
  543.   else
  544.     {
  545.       x_max = i_x;
  546.       x_min = r_x;
  547.     }
  548.  
  549.   set_complex_format (sign, x_max, x_min, r_x, inf_or_nan, int_only,
  550.               r_fw, i_fw);
  551. }
  552.  
  553. static inline void
  554. set_format (const Complex& c)
  555. {
  556.   int r_fw, i_fw;
  557.   set_format (c, r_fw, i_fw);
  558. }
  559.  
  560. static void
  561. set_complex_matrix_format (bool sign, int x_max, int x_min,
  562.                int r_x_max, int r_x_min, bool inf_or_nan,
  563.                int int_or_inf_or_nan, int& r_fw, int& i_fw)
  564. {
  565.   static char r_fmt_buf[128];
  566.   static char i_fmt_buf[128];
  567.  
  568.   int prec = Voutput_precision;
  569.  
  570.   int ld, rd;
  571.  
  572.   if (bank_format)
  573.     {
  574.       int digits = r_x_max > r_x_min ? r_x_max : r_x_min;
  575.       i_fw = 0;
  576.       r_fw = digits <= 0 ? 4 : digits + 3;
  577.       if (inf_or_nan && i_fw < 3)
  578.     i_fw = r_fw = 3;
  579.       r_fw += sign;
  580.       rd = 2;
  581.     }
  582.   else if (hex_format)
  583.     {
  584.       r_fw = 2 * sizeof (double);
  585.       i_fw = 2 * sizeof (double);
  586.       rd = 0;
  587.     }
  588.   else if (bit_format)
  589.     {
  590.       r_fw = 8 * sizeof (double);
  591.       i_fw = 8 * sizeof (double);
  592.       rd = 0;
  593.     }
  594.   else if (int_or_inf_or_nan)
  595.     {
  596.       int digits = x_max > x_min ? x_max : x_min;
  597.       i_fw = r_fw = digits <= 0 ? 1 : digits;
  598.       if (inf_or_nan && i_fw < 3)
  599.     i_fw = r_fw = 3;
  600.       r_fw += sign;
  601.       rd = 0;
  602.     }
  603.   else
  604.     {
  605.       int ld_max, rd_max;
  606.       if (x_max > 0)
  607.     {
  608.       ld_max = x_max;
  609.       rd_max = prec > x_max ? prec - x_max : prec;
  610.       x_max++;
  611.     }
  612.       else
  613.     {
  614.       ld_max = 1;
  615.       rd_max = prec > x_max ? prec - x_max : prec;
  616.       x_max = -x_max + 1;
  617.     }
  618.  
  619.       int ld_min, rd_min;
  620.       if (x_min > 0)
  621.     {
  622.       ld_min = x_min;
  623.       rd_min = prec > x_min ? prec - x_min : prec;
  624.       x_min++;
  625.     }
  626.       else
  627.     {
  628.       ld_min = 1;
  629.       rd_min = prec > x_min ? prec - x_min : prec;
  630.       x_min = -x_min + 1;
  631.     }
  632.  
  633.       ld = ld_max > ld_min ? ld_max : ld_min;
  634.       rd = rd_max > rd_min ? rd_max : rd_min;
  635.  
  636.       i_fw = r_fw = ld + 1 + rd;
  637.       if (inf_or_nan && i_fw < 3)
  638.     i_fw = r_fw = 3;
  639.       r_fw += sign;
  640.     }
  641.  
  642.   if (! (bank_format || hex_format || bit_format)
  643.       && (r_fw > Voutput_max_field_width || print_e))
  644.     {
  645.       int exp_field = 4;
  646.       if (x_max > 100 || x_min > 100)
  647.     exp_field++;
  648.  
  649.       i_fw = r_fw = 1 + prec + exp_field;
  650.       if (inf_or_nan && i_fw < 3)
  651.     i_fw = r_fw = 3;
  652.       r_fw += sign;
  653.  
  654.       if (print_big_e)
  655.     {
  656.       sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1);
  657.       sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1);
  658.     }
  659.       else
  660.     {
  661.       sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1);
  662.       sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1);
  663.     }
  664.     }
  665.   else
  666.     {
  667.       sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd);
  668.       sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd);
  669.     }
  670.  
  671.   curr_real_fmt = &r_fmt_buf[0];
  672.   curr_imag_fmt = &i_fmt_buf[0];
  673. }
  674.  
  675. static void
  676. set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw)
  677. {
  678.   curr_real_fmt = 0;
  679.   curr_imag_fmt = 0;
  680.  
  681.   if (free_format)
  682.     return;
  683.  
  684.   Matrix rp = real (cm);
  685.   Matrix ip = imag (cm);
  686.  
  687.   bool sign = rp.any_element_is_negative ();
  688.  
  689.   bool inf_or_nan = cm.any_element_is_inf_or_nan ();
  690.  
  691.   bool int_or_inf_or_nan = (rp.all_elements_are_int_or_inf_or_nan ()
  692.                 && ip.all_elements_are_int_or_inf_or_nan ());
  693.  
  694.   Matrix r_m_abs = rp.abs ();
  695.   double r_max_abs = pr_max_internal (r_m_abs);
  696.   double r_min_abs = pr_min_internal (r_m_abs);
  697.  
  698.   Matrix i_m_abs = ip.abs ();
  699.   double i_max_abs = pr_max_internal (i_m_abs);
  700.   double i_min_abs = pr_min_internal (i_m_abs);
  701.  
  702.   int r_x_max = r_max_abs == 0.0 ? 0 : (int) floor (log10 (r_max_abs) + 1.0);
  703.   int r_x_min = r_min_abs == 0.0 ? 0 : (int) floor (log10 (r_min_abs) + 1.0);
  704.  
  705.   int i_x_max = i_max_abs == 0.0 ? 0 : (int) floor (log10 (i_max_abs) + 1.0);
  706.   int i_x_min = i_min_abs == 0.0 ? 0 : (int) floor (log10 (i_min_abs) + 1.0);
  707.  
  708.   int x_max = r_x_max > i_x_max ? r_x_max : i_x_max;
  709.   int x_min = r_x_min > i_x_min ? r_x_min : i_x_min;
  710.  
  711.   set_complex_matrix_format (sign, x_max, x_min, r_x_max, r_x_min,
  712.                  inf_or_nan, int_or_inf_or_nan, r_fw, i_fw);
  713. }
  714.  
  715. static inline void
  716. set_format (const ComplexMatrix& cm)
  717. {
  718.   int r_fw, i_fw;
  719.   set_format (cm, r_fw, i_fw);
  720. }
  721.  
  722. static void
  723. set_range_format (bool sign, int x_max, int x_min, int all_ints, int& fw)
  724. {
  725.   static char fmt_buf[128];
  726.  
  727.   int prec = Voutput_precision;
  728.  
  729.   int ld, rd;
  730.  
  731.   if (bank_format)
  732.     {
  733.       int digits = x_max > x_min ? x_max : x_min;
  734.       fw = sign + digits < 0 ? 4 : digits + 3;
  735.       rd = 2;
  736.     }
  737.   else if (hex_format)
  738.     {
  739.       fw = 2 * sizeof (double);
  740.       rd = 0;
  741.     }
  742.   else if (bit_format)
  743.     {
  744.       fw = 8 * sizeof (double);
  745.       rd = 0;
  746.     }
  747.   else if (all_ints)
  748.     {
  749.       int digits = x_max > x_min ? x_max : x_min;
  750.       fw = sign + digits;
  751.       rd = 0;
  752.     }
  753.   else
  754.     {
  755.       int ld_max, rd_max;
  756.       if (x_max > 0)
  757.     {
  758.       ld_max = x_max;
  759.       rd_max = prec > x_max ? prec - x_max : prec;
  760.       x_max++;
  761.     }
  762.       else
  763.     {
  764.       ld_max = 1;
  765.       rd_max = prec > x_max ? prec - x_max : prec;
  766.       x_max = -x_max + 1;
  767.     }
  768.  
  769.       int ld_min, rd_min;
  770.       if (x_min > 0)
  771.     {
  772.       ld_min = x_min;
  773.       rd_min = prec > x_min ? prec - x_min : prec;
  774.       x_min++;
  775.     }
  776.       else
  777.     {
  778.       ld_min = 1;
  779.       rd_min = prec > x_min ? prec - x_min : prec;
  780.       x_min = -x_min + 1;
  781.     }
  782.  
  783.       ld = ld_max > ld_min ? ld_max : ld_min;
  784.       rd = rd_max > rd_min ? rd_max : rd_min;
  785.  
  786.       fw = sign + ld + 1 + rd;
  787.     }
  788.  
  789.   if (! (bank_format || hex_format || bit_format)
  790.       && (fw > Voutput_max_field_width || print_e))
  791.     {
  792.       int exp_field = 4;
  793.       if (x_max > 100 || x_min > 100)
  794.     exp_field++;
  795.  
  796.       fw = sign + 2 + prec + exp_field;
  797.  
  798.       if (print_big_e)
  799.     sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1);
  800.       else
  801.     sprintf (fmt_buf, "%%%d.%de", fw, prec - 1);
  802.     }
  803.   else
  804.     {
  805.       sprintf (fmt_buf, "%%%d.%df", fw, rd);
  806.     }
  807.  
  808.   curr_real_fmt = &fmt_buf[0];
  809. }
  810.  
  811. static void
  812. set_format (const Range& r, int& fw)
  813. {
  814.   curr_real_fmt = 0;
  815.   curr_imag_fmt = 0;
  816.  
  817.   if (free_format)
  818.     return;
  819.  
  820.   double r_min = r.base ();
  821.   double r_max = r.limit ();
  822.  
  823.   if (r_max < r_min)
  824.     {
  825.       double tmp = r_max;
  826.       r_max = r_min;
  827.       r_min = tmp;
  828.     }
  829.  
  830.   bool sign = (r_min < 0.0);
  831.  
  832.   bool all_ints = r.all_elements_are_ints ();
  833.  
  834.   double max_abs = r_max < 0.0 ? -r_max : r_max;
  835.   double min_abs = r_min < 0.0 ? -r_min : r_min;
  836.  
  837.   int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0);
  838.   int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0);
  839.  
  840.   set_range_format (sign, x_max, x_min, all_ints, fw);
  841. }
  842.  
  843. static inline void
  844. set_format (const Range& r)
  845. {
  846.   int fw;
  847.   set_format (r, fw);
  848. }
  849.  
  850. union equiv
  851. {
  852.   double d;
  853.   unsigned char i[sizeof (double)];
  854. };
  855.  
  856. #define PRINT_CHAR_BITS(os, c) \
  857.   do \
  858.     { \
  859.       unsigned char ctmp = c; \
  860.       char stmp[9]; \
  861.       stmp[0] = (ctmp & 0x80) ? '1' : '0'; \
  862.       stmp[1] = (ctmp & 0x40) ? '1' : '0'; \
  863.       stmp[2] = (ctmp & 0x20) ? '1' : '0'; \
  864.       stmp[3] = (ctmp & 0x10) ? '1' : '0'; \
  865.       stmp[4] = (ctmp & 0x08) ? '1' : '0'; \
  866.       stmp[5] = (ctmp & 0x04) ? '1' : '0'; \
  867.       stmp[6] = (ctmp & 0x02) ? '1' : '0'; \
  868.       stmp[7] = (ctmp & 0x01) ? '1' : '0'; \
  869.       stmp[8] = '\0'; \
  870.       os.form ("%s", stmp); \
  871.     } \
  872.   while (0)
  873.  
  874. #define PRINT_CHAR_BITS_SWAPPED(os, c) \
  875.   do \
  876.     { \
  877.       unsigned char ctmp = c; \
  878.       char stmp[9]; \
  879.       stmp[0] = (ctmp & 0x01) ? '1' : '0'; \
  880.       stmp[1] = (ctmp & 0x02) ? '1' : '0'; \
  881.       stmp[2] = (ctmp & 0x04) ? '1' : '0'; \
  882.       stmp[3] = (ctmp & 0x08) ? '1' : '0'; \
  883.       stmp[4] = (ctmp & 0x10) ? '1' : '0'; \
  884.       stmp[5] = (ctmp & 0x20) ? '1' : '0'; \
  885.       stmp[6] = (ctmp & 0x40) ? '1' : '0'; \
  886.       stmp[7] = (ctmp & 0x80) ? '1' : '0'; \
  887.       stmp[8] = '\0'; \
  888.       os.form ("%s", stmp); \
  889.     } \
  890.   while (0)
  891.  
  892. static void
  893. pr_any_float (const char *fmt, ostream& os, double d, int fw = 0)
  894. {
  895. #if defined (SCO)
  896.   // Apparently on some SCO systems NaN == -0.0 is true.  Compiler bug?
  897.   if (d == -0.0 && ! xisnan (d))
  898.     d = 0.0;
  899. #else
  900.   if (d == -0.0)
  901.     d = 0.0;
  902. #endif
  903.  
  904.   if (fmt)
  905.     {
  906.       if (hex_format)
  907.     {
  908.       equiv tmp;
  909.       tmp.d = d;
  910.  
  911.       // Unless explicitly asked for, always print in big-endian
  912.       // format.
  913.  
  914.       // XXX FIXME XXX -- is it correct to swap bytes for VAX
  915.       // formats and not for Cray?
  916.  
  917.       oct_mach_info::float_format flt_fmt =
  918.         oct_mach_info::native_float_format ();
  919.  
  920.       if (hex_format > 1
  921.           || flt_fmt == oct_mach_info::ieee_big_endian
  922.           || flt_fmt == oct_mach_info::cray
  923.           || flt_fmt == oct_mach_info::unknown)
  924.         {
  925.           for (size_t i = 0; i < sizeof (double); i++)
  926.         os.form ("%02x", (int) tmp.i[i]);
  927.         }
  928.       else
  929.         {
  930.           for (int i = sizeof (double) - 1; i >= 0; i--)
  931.         os.form ("%02x", (int) tmp.i[i]);
  932.         }
  933.     }
  934.       else if (bit_format)
  935.     {
  936.       equiv tmp;
  937.       tmp.d = d;
  938.  
  939.       // Unless explicitly asked for, always print in big-endian
  940.       // format.
  941.  
  942.       // XXX FIXME XXX -- is it correct to swap bytes for VAX
  943.       // formats and not for Cray?
  944.  
  945.       oct_mach_info::float_format flt_fmt =
  946.         oct_mach_info::native_float_format ();
  947.  
  948.       if (flt_fmt == oct_mach_info::ieee_big_endian
  949.           || flt_fmt == oct_mach_info::cray
  950.           || flt_fmt == oct_mach_info::unknown)
  951.         {
  952.           for (size_t i = 0; i < sizeof (double); i++)
  953.         PRINT_CHAR_BITS (os, tmp.i[i]);
  954.         }
  955.       else
  956.         {
  957.           if (bit_format > 1)
  958.         {
  959.           for (size_t i = 0; i < sizeof (double); i++)
  960.             PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]);
  961.         }
  962.           else
  963.         {
  964.           for (int i = sizeof (double) - 1; i >= 0; i--)
  965.             PRINT_CHAR_BITS (os, tmp.i[i]);
  966.         }
  967.         }
  968.     }
  969.       else if (xisinf (d))
  970.     {
  971.       char *s;
  972.       if (d < 0.0)
  973.         s = "-Inf";
  974.       else
  975.         s = "Inf";
  976.  
  977.       if (fw > 0)
  978.         os.form ("%*s", fw, s);
  979.       else
  980.         os << s;
  981.     }
  982.       else if (xisnan (d))
  983.     {
  984.       if (fw > 0)
  985.         os.form ("%*s", fw, "NaN");
  986.       else
  987.         os << "NaN";
  988.     }
  989.       else
  990.     os.form (fmt, d);
  991.     }
  992.   else
  993.     os << d;
  994. }
  995.  
  996. static inline void
  997. pr_float (ostream& os, double d, int fw = 0)
  998. {
  999.   pr_any_float (curr_real_fmt, os, d, fw);
  1000. }
  1001.  
  1002. static inline void
  1003. pr_imag_float (ostream& os, double d, int fw = 0)
  1004. {
  1005.   pr_any_float (curr_imag_fmt, os, d, fw);
  1006. }
  1007.  
  1008. static void
  1009. pr_complex (ostream& os, const Complex& c, int r_fw = 0, int i_fw = 0)
  1010. {
  1011.   double r = c.real ();
  1012.   pr_float (os, r, r_fw);
  1013.   if (! bank_format)
  1014.     {
  1015.       double i = c.imag ();
  1016.       if (! (hex_format || bit_format) && i < 0)
  1017.     {
  1018.       os << " - ";
  1019.       i = -i;
  1020.       pr_imag_float (os, i, i_fw);
  1021.     }
  1022.       else
  1023.     {
  1024.       if (hex_format || bit_format)
  1025.         os << "  ";
  1026.       else
  1027.         os << " + ";
  1028.  
  1029.       pr_imag_float (os, i, i_fw);
  1030.     }
  1031.       os << "i";
  1032.     }
  1033. }
  1034.  
  1035. static void
  1036. print_empty_matrix (ostream& os, int nr, int nc, bool pr_as_read_syntax)
  1037. {
  1038.   assert (nr == 0 || nc == 0);
  1039.  
  1040.   if (pr_as_read_syntax)
  1041.     {
  1042.       if (nr == 0 && nc == 0)
  1043.     os << "[]";
  1044.       else
  1045.     os << "zeros (" << nr << ", " << nc << ")";
  1046.     }
  1047.   else
  1048.     {
  1049.       os << "[]";
  1050.       if (Vprint_empty_dimensions)
  1051.     os << "(" << nr << "x" << nc << ")";
  1052.       os << "\n";
  1053.     }
  1054. }
  1055.  
  1056. static void
  1057. pr_col_num_header (ostream& os, int total_width, int max_width,
  1058.            int lim, int col, int extra_indent)
  1059. {
  1060.   if (total_width > max_width && Vsplit_long_rows)
  1061.     {
  1062.       if (col != 0 && ! compact_format)
  1063.     os << "\n";
  1064.  
  1065.       int num_cols = lim - col;
  1066.  
  1067.       os.form ("%*s", extra_indent, "");
  1068.  
  1069.       if (num_cols == 1)
  1070.     os << " Column " << col + 1 << ":\n";
  1071.       else if (num_cols == 2)
  1072.     os << " Columns " << col + 1 << " and " << lim << ":\n";
  1073.       else
  1074.     os << " Columns " << col + 1 << " through " << lim << ":\n";
  1075.  
  1076.       if (! compact_format)
  1077.     os << "\n";
  1078.     }
  1079. }
  1080.  
  1081. void
  1082. octave_print_internal (ostream& os, double d, bool pr_as_read_syntax)
  1083. {
  1084.   if (plus_format)
  1085.     {
  1086.       if (d == 0.0)
  1087.     os << " ";
  1088.       else
  1089.     os << "+";
  1090.     }
  1091.   else
  1092.     {
  1093.       set_format (d);
  1094.       if (free_format)
  1095.     os << d;
  1096.       else
  1097.     pr_float (os, d);
  1098.     }
  1099.  
  1100.   if (! pr_as_read_syntax)
  1101.     os << "\n";
  1102. }
  1103.  
  1104. void
  1105. octave_print_internal (ostream& os, const Matrix& m, bool pr_as_read_syntax,
  1106.                int extra_indent)
  1107. {
  1108.   int nr = m.rows ();
  1109.   int nc = m.columns ();
  1110.  
  1111.   if (nr == 0 || nc == 0)
  1112.     print_empty_matrix (os, nr, nc, pr_as_read_syntax);
  1113.   else if (plus_format && ! pr_as_read_syntax)
  1114.     {
  1115.       for (int i = 0; i < nr; i++)
  1116.     {
  1117.       for (int j = 0; j < nc; j++)
  1118.         {
  1119.           if (j == 0)
  1120.         os << "  ";
  1121.  
  1122.           if (m (i, j) == 0.0)
  1123.         os << " ";
  1124.           else
  1125.         os << "+";
  1126.         }
  1127.       os << "\n";
  1128.     }
  1129.     }
  1130.   else
  1131.     {
  1132.       int fw;
  1133.       set_format (m, fw);
  1134.       int column_width = fw + 2;
  1135.       int total_width = nc * column_width;
  1136.       int max_width = terminal_columns ();
  1137.  
  1138.       if (pr_as_read_syntax)
  1139.     max_width -= 4;
  1140.       else
  1141.     max_width -= extra_indent;
  1142.  
  1143.       if (max_width < 0)
  1144.     max_width = 0;
  1145.  
  1146.       if (free_format)
  1147.     {
  1148.       if (pr_as_read_syntax)
  1149.         os << "[\n";
  1150.  
  1151.       os << m;
  1152.  
  1153.       if (pr_as_read_syntax)
  1154.         os << "]";
  1155.  
  1156.       return;
  1157.     }
  1158.  
  1159.       int inc = nc;
  1160.       if (total_width > max_width && Vsplit_long_rows)
  1161.     {
  1162.       inc = max_width / column_width;
  1163.       if (inc == 0)
  1164.         inc++;
  1165.     }
  1166.  
  1167.       if (pr_as_read_syntax)
  1168.     {
  1169.       for (int i = 0; i < nr; i++)
  1170.         {
  1171.           int col = 0;
  1172.           while (col < nc)
  1173.         {
  1174.           int lim = col + inc < nc ? col + inc : nc;
  1175.  
  1176.           for (int j = col; j < lim; j++)
  1177.             {
  1178.               if (i == 0 && j == 0)
  1179.             os << "[ ";
  1180.               else
  1181.             {
  1182.               if (j > col && j < lim)
  1183.                 os << ", ";
  1184.               else
  1185.                 os << "  ";
  1186.             }
  1187.  
  1188.               pr_float (os, m (i, j));
  1189.             }
  1190.  
  1191.           col += inc;
  1192.  
  1193.           if (col >= nc)
  1194.             {
  1195.               if (i == nr - 1)
  1196.             os << " ]";
  1197.               else
  1198.             os << ";\n";
  1199.             }
  1200.           else
  1201.             os << " ...\n";
  1202.         }
  1203.         }
  1204.     }
  1205.       else
  1206.     {
  1207.       for (int col = 0; col < nc; col += inc)
  1208.         {
  1209.           int lim = col + inc < nc ? col + inc : nc;
  1210.  
  1211.           pr_col_num_header (os, total_width, max_width, lim, col,
  1212.                  extra_indent);
  1213.  
  1214.           for (int i = 0; i < nr; i++)
  1215.         {
  1216.           os.form ("%*s", extra_indent, "");
  1217.  
  1218.           for (int j = col; j < lim; j++)
  1219.             {
  1220.               os << "  ";
  1221.  
  1222.               pr_float (os, m (i, j), fw);
  1223.             }
  1224.  
  1225.           os << "\n";
  1226.         }
  1227.         }
  1228.     }
  1229.     }
  1230. }
  1231.  
  1232. void
  1233. octave_print_internal (ostream& os, const Complex& c,
  1234.                bool pr_as_read_syntax)
  1235. {
  1236.   if (plus_format)
  1237.     {
  1238.       if (c == 0.0)
  1239.     os << " ";
  1240.       else
  1241.     os << "+";
  1242.     }
  1243.   else
  1244.     {
  1245.       set_format (c);
  1246.       if (free_format)
  1247.     os << c;
  1248.       else
  1249.     pr_complex (os, c);
  1250.     }
  1251.  
  1252.   if (! pr_as_read_syntax)
  1253.     os << "\n";
  1254. }
  1255.  
  1256. void
  1257. octave_print_internal (ostream& os, const ComplexMatrix& cm,
  1258.                bool pr_as_read_syntax, int extra_indent)
  1259. {
  1260.   int nr = cm.rows ();
  1261.   int nc = cm.columns ();
  1262.  
  1263.  if (nr == 0 || nc == 0)
  1264.     print_empty_matrix (os, nr, nc, pr_as_read_syntax);
  1265.   else if (plus_format && ! pr_as_read_syntax)
  1266.     {
  1267.       for (int i = 0; i < nr; i++)
  1268.     {
  1269.       for (int j = 0; j < nc; j++)
  1270.         {
  1271.           if (j == 0)
  1272.         os << "  ";
  1273.  
  1274.           if (cm (i, j) == 0.0)
  1275.         os << " ";
  1276.           else
  1277.         os << "+";
  1278.         }
  1279.       os << "\n";
  1280.     }
  1281.     }
  1282.   else
  1283.     {
  1284.       int r_fw, i_fw;
  1285.       set_format (cm, r_fw, i_fw);
  1286.       int column_width = i_fw + r_fw;
  1287.       column_width += (bank_format || hex_format|| bit_format) ? 2 : 7;
  1288.       int total_width = nc * column_width;
  1289.       int max_width = terminal_columns ();
  1290.  
  1291.       if (pr_as_read_syntax)
  1292.     max_width -= 4;
  1293.       else
  1294.     max_width -= extra_indent;
  1295.  
  1296.       if (max_width < 0)
  1297.     max_width = 0;
  1298.  
  1299.       if (free_format)
  1300.     {
  1301.       if (pr_as_read_syntax)
  1302.         os << "[\n";
  1303.  
  1304.       os << cm;
  1305.  
  1306.       if (pr_as_read_syntax)
  1307.         os << "]";
  1308.  
  1309.       return;
  1310.     }
  1311.  
  1312.       int inc = nc;
  1313.       if (total_width > max_width && Vsplit_long_rows)
  1314.     {
  1315.       inc = max_width / column_width;
  1316.       if (inc == 0)
  1317.         inc++;
  1318.     }
  1319.  
  1320.       if (pr_as_read_syntax)
  1321.     {
  1322.       for (int i = 0; i < nr; i++)
  1323.         {
  1324.           int col = 0;
  1325.           while (col < nc)
  1326.         {
  1327.           int lim = col + inc < nc ? col + inc : nc;
  1328.  
  1329.           for (int j = col; j < lim; j++)
  1330.             {
  1331.               if (i == 0 && j == 0)
  1332.             os << "[ ";
  1333.               else
  1334.             {
  1335.               if (j > col && j < lim)
  1336.                 os << ", ";
  1337.               else
  1338.                 os << "  ";
  1339.             }
  1340.  
  1341.               pr_complex (os, cm (i, j));
  1342.             }
  1343.  
  1344.           col += inc;
  1345.  
  1346.           if (col >= nc)
  1347.             {
  1348.               if (i == nr - 1)
  1349.             os << " ]";
  1350.               else
  1351.             os << ";\n";
  1352.             }
  1353.           else
  1354.             os << " ...\n";
  1355.         }
  1356.         }
  1357.     }
  1358.       else
  1359.     {
  1360.       for (int col = 0; col < nc; col += inc)
  1361.         {
  1362.           int lim = col + inc < nc ? col + inc : nc;
  1363.  
  1364.           pr_col_num_header (os, total_width, max_width, lim, col,
  1365.                  extra_indent);
  1366.  
  1367.           for (int i = 0; i < nr; i++)
  1368.         {
  1369.           os.form ("%*s", extra_indent, "");
  1370.  
  1371.           for (int j = col; j < lim; j++)
  1372.             {
  1373.               os << "  ";
  1374.  
  1375.               pr_complex (os, cm (i, j));
  1376.             }
  1377.           os << "\n";
  1378.         }
  1379.         }
  1380.     }
  1381.     }
  1382. }
  1383.  
  1384. void
  1385. octave_print_internal (ostream& os, const Range& r,
  1386.                bool pr_as_read_syntax, int extra_indent)
  1387. {
  1388.   double base = r.base ();
  1389.   double increment = r.inc ();
  1390.   double limit = r.limit ();
  1391.   int num_elem = r.nelem ();
  1392.  
  1393.   if (plus_format && ! pr_as_read_syntax)
  1394.     {
  1395.       os << "  ";
  1396.       for (int i = 0; i < num_elem; i++)
  1397.     {
  1398.       double val = base + i * increment;
  1399.       if (val == 0.0)
  1400.         os << " ";
  1401.       else
  1402.         os << "+";
  1403.     }
  1404.     }
  1405.   else
  1406.     {
  1407.       int fw;
  1408.       set_format (r, fw);
  1409.  
  1410.       if (pr_as_read_syntax)
  1411.     {
  1412.       if (free_format)
  1413.         {
  1414.           os << base << " : ";
  1415.           if (increment != 1.0)
  1416.         os << increment << " : ";
  1417.           os << limit;
  1418.         }
  1419.       else
  1420.         {
  1421.           pr_float (os, base, fw);
  1422.           os << " : ";
  1423.           if (increment != 1.0)
  1424.         {
  1425.           pr_float (os, increment, fw);
  1426.           os << " : ";
  1427.         }
  1428.           pr_float (os, limit, fw);
  1429.         }
  1430.     }
  1431.       else
  1432.     {
  1433.       int column_width = fw + 2;
  1434.       int total_width = num_elem * column_width;
  1435.       int max_width = terminal_columns ();
  1436.  
  1437.       if (free_format)
  1438.         {
  1439.           os << r;
  1440.           return;
  1441.         }
  1442.  
  1443.       int inc = num_elem;
  1444.       if (total_width > max_width && Vsplit_long_rows)
  1445.         {
  1446.           inc = max_width / column_width;
  1447.           if (inc == 0)
  1448.         inc++;
  1449.         }
  1450.  
  1451.       max_width -= extra_indent;
  1452.  
  1453.       if (max_width < 0)
  1454.         max_width = 0;
  1455.  
  1456.       int col = 0;
  1457.       while (col < num_elem)
  1458.         {
  1459.           int lim = col + inc < num_elem ? col + inc : num_elem;
  1460.  
  1461.           pr_col_num_header (os, total_width, max_width, lim, col,
  1462.                  extra_indent);
  1463.  
  1464.           os.form ("%*s", extra_indent, "");
  1465.  
  1466.           for (int i = col; i < lim; i++)
  1467.         {
  1468.           double val = base + i * increment;
  1469.           os << "  ";
  1470.           pr_float (os, val, fw);
  1471.         }
  1472.  
  1473.           os << "\n";
  1474.  
  1475.           col += inc;
  1476.         }
  1477.     }
  1478.     }
  1479. }
  1480.  
  1481. void
  1482. octave_print_internal (ostream& os, const charMatrix& chm,
  1483.                bool pr_as_read_syntax, bool pr_as_string,
  1484.                int /* extra_indent XXX FIXME XXX */)
  1485. {
  1486.   if (pr_as_string)
  1487.     {
  1488.       int nstr = chm.rows ();
  1489.  
  1490.       if (pr_as_read_syntax && nstr > 1)
  1491.     os << "[ ";
  1492.  
  1493.       if (nstr == 0)
  1494.     os << "\n";
  1495.       else
  1496.     {
  1497.       for (int i = 0; i < nstr; i++)
  1498.         {
  1499.           string row = chm.row_as_string (i);
  1500.  
  1501.           if (pr_as_read_syntax)
  1502.         {
  1503.           os << "\"" << undo_string_escapes (row) << "\"";
  1504.  
  1505.           if (i < nstr - 1)
  1506.             os << "; ";
  1507.         }
  1508.           else
  1509.         os << row << "\n";
  1510.         }
  1511.     }
  1512.  
  1513.       if (pr_as_read_syntax && nstr > 1)
  1514.     os << " ]";
  1515.     }
  1516.   else
  1517.     {
  1518.       os << "sorry, printing char matrices not implemented yet\n";
  1519.     }
  1520. }
  1521.  
  1522. DEFUN (disp, args, ,
  1523.   "disp (X): display value without name tag")
  1524. {
  1525.   octave_value_list retval;
  1526.  
  1527.   int nargin = args.length ();
  1528.  
  1529.   if (nargin == 1)
  1530.     args(0).print ();
  1531.   else
  1532.     print_usage ("disp");
  1533.  
  1534.   return retval;
  1535. }
  1536.  
  1537. static void
  1538. init_format_state (void)
  1539. {
  1540.   free_format = false;
  1541.   plus_format = false;
  1542.   bank_format = false;
  1543.   hex_format = false;
  1544.   bit_format = 0;
  1545.   print_e = false;
  1546.   print_big_e = false;
  1547. }
  1548.  
  1549. static void
  1550. set_output_prec_and_fw (int prec, int fw)
  1551. {
  1552.   bind_builtin_variable ("output_precision", (double) prec);
  1553.   bind_builtin_variable ("output_max_field_width", (double) fw);
  1554. }
  1555.  
  1556. static void
  1557. set_format_style (int argc, const string_vector& argv)
  1558. {
  1559.   int idx = 1;
  1560.  
  1561.   if (--argc > 0)
  1562.     {
  1563.       string arg = argv[idx++];
  1564.  
  1565.       if (arg == "short")
  1566.     {
  1567.       if (--argc > 0)
  1568.         {
  1569.           arg = argv[idx++];
  1570.  
  1571.           if (arg == "e")
  1572.         {
  1573.           init_format_state ();
  1574.           print_e = true;
  1575.         }
  1576.           else if (arg == "E")
  1577.         {
  1578.           init_format_state ();
  1579.           print_e = true;
  1580.           print_big_e = true;
  1581.         }
  1582.           else
  1583.         {
  1584.           error ("format: unrecognized option `short %s'",
  1585.              arg.c_str ());
  1586.           return;
  1587.         }
  1588.         }
  1589.       else
  1590.         init_format_state ();
  1591.  
  1592.       set_output_prec_and_fw (3, 8);
  1593.     }
  1594.       else if (arg == "long")
  1595.     {
  1596.       if (--argc > 0)
  1597.         {
  1598.           arg = argv[idx++];
  1599.  
  1600.           if (arg == "e")
  1601.         {
  1602.           init_format_state ();
  1603.           print_e = true;
  1604.         }
  1605.           else if (arg == "E")
  1606.         {
  1607.           init_format_state ();
  1608.           print_e = true;
  1609.           print_big_e = true;
  1610.         }
  1611.           else
  1612.         {
  1613.           error ("format: unrecognized option `long %s'",
  1614.              arg.c_str ());
  1615.           return;
  1616.         }
  1617.         }
  1618.       else
  1619.         init_format_state ();
  1620.  
  1621.       set_output_prec_and_fw (15, 24);
  1622.     }
  1623.       else if (arg == "hex")
  1624.     {
  1625.       init_format_state ();
  1626.       hex_format = true;
  1627.     }
  1628.       else if (arg == "native-hex")
  1629.     {
  1630.       init_format_state ();
  1631.       hex_format = 2;
  1632.     }
  1633.       else if (arg == "bit")
  1634.     {
  1635.       init_format_state ();
  1636.       bit_format = 1;
  1637.     }
  1638.       else if (arg == "native-bit")
  1639.     {
  1640.       init_format_state ();
  1641.       bit_format = 2;
  1642.     }
  1643.       else if (arg == "+" || arg == "plus")
  1644.     {
  1645.       init_format_state ();
  1646.       plus_format = true;
  1647.     }
  1648.       else if (arg == "bank")
  1649.     {
  1650.       init_format_state ();
  1651.       bank_format = true;
  1652.     }
  1653.       else if (arg == "free")
  1654.     {
  1655.       init_format_state ();
  1656.       free_format = true;
  1657.     }
  1658.       else if (arg == "none")
  1659.     {
  1660.       init_format_state ();
  1661.       free_format = true;
  1662.     }
  1663.       else if (arg == "compact")
  1664.     {
  1665.       compact_format = true;
  1666.     }
  1667.       else if (arg == "loose")
  1668.     {
  1669.       compact_format = false;
  1670.     }
  1671.       else
  1672.     error ("format: unrecognized format state `%s'", arg.c_str ());
  1673.     }
  1674.   else
  1675.     {
  1676.       init_format_state ();
  1677.       set_output_prec_and_fw (5, 10);
  1678.     }
  1679. }
  1680.  
  1681. DEFUN_TEXT (format, args, ,
  1682.   "format [style]\n\
  1683. \n\
  1684. set output formatting style")
  1685. {
  1686.   octave_value_list retval;
  1687.  
  1688.   int argc = args.length () + 1;
  1689.  
  1690.   string_vector argv = args.make_argv ("format");
  1691.  
  1692.   if (error_state)
  1693.     return retval;
  1694.  
  1695.   set_format_style (argc, argv);
  1696.  
  1697.   return retval;
  1698. }
  1699.  
  1700. static int
  1701. output_max_field_width (void)
  1702. {
  1703.   double val;
  1704.   if (builtin_real_scalar_variable ("output_max_field_width", val)
  1705.       && ! xisnan (val))
  1706.     {
  1707.       int ival = NINT (val);
  1708.       if (ival > 0 && (double) ival == val)
  1709.     {
  1710.       Voutput_max_field_width = ival;
  1711.       return 0;
  1712.     }
  1713.     }
  1714.   gripe_invalid_value_specified ("output_max_field_width");
  1715.   return -1;
  1716. }
  1717.  
  1718. static int
  1719. output_precision (void)
  1720. {
  1721.   double val;
  1722.   if (builtin_real_scalar_variable ("output_precision", val)
  1723.       && ! xisnan (val))
  1724.     {
  1725.       int ival = NINT (val);
  1726.       if (ival >= 0 && (double) ival == val)
  1727.     {
  1728.       Voutput_precision = ival;
  1729.       return 0;
  1730.     }
  1731.     }
  1732.   gripe_invalid_value_specified ("output_precision");
  1733.   return -1;
  1734. }
  1735.  
  1736. static int
  1737. print_empty_dimensions (void)
  1738. {
  1739.   Vprint_empty_dimensions = check_preference ("print_empty_dimensions");
  1740.  
  1741.   return 0;
  1742. }
  1743.  
  1744. static int
  1745. split_long_rows (void)
  1746. {
  1747.   Vsplit_long_rows = check_preference ("split_long_rows");
  1748.  
  1749.   return 0;
  1750. }
  1751.  
  1752. void
  1753. symbols_of_pr_output (void)
  1754. {
  1755.   DEFVAR (output_max_field_width, 10.0, 0, output_max_field_width,
  1756.     "maximum width of an output field for numeric output");
  1757.  
  1758.   DEFVAR (output_precision, 5.0, 0, output_precision,
  1759.     "number of significant figures to display for numeric output");
  1760.  
  1761.   DEFVAR (print_empty_dimensions, 1.0, 0, print_empty_dimensions,
  1762.     "also print dimensions of empty matrices");
  1763.  
  1764.   DEFVAR (split_long_rows, 1.0, 0, split_long_rows,
  1765.     "split long matrix rows instead of wrapping");
  1766. }
  1767.  
  1768. /*
  1769. ;;; Local Variables: ***
  1770. ;;; mode: C++ ***
  1771. ;;; End: ***
  1772. */
  1773.